/*
	Author: Drew Risinger, Chris Cochoff, Jordan Walsh, Cameron Holloway
	Course: COMP 220, Computer Programming II
	Date: February 24, 2014
	Description: This file implements the methods for a Deck
		This includes creating, shuffling, and dealing a card
*/
#include "Deck.h"
#include <vector>
#include <ctime>
#define DEFAULT_DECKS 4
#define NUMBER_SWAPS 2000
//Constructor for Deck. Use DEFAULT_DECKS to define number of 52-card decks
//must call constructor for each card in vector.
//saves to Vector<Card> deck
//then shuffles
Deck::Deck(){
	Value currentValue;
	Suit currentSuit;
	for(int i=0; i<DEFAULT_DECKS; i++){
		for(int s=clubs; s<=spades; s++){
			currentSuit=static_cast<Suit>(s);
			for(int v=deuce; v<=ace; v++){
				currentValue=static_cast<Value>(v);
				deck.push_back(Card(currentValue,currentSuit));
			}
		}
	}
	srand(time(0));
	deckLength=deck.size();
	currentCard=0;
	shuffle();
}

//returns card from Deck. Does not change deck, just moves pointer (currentCard) to next
//Must check if pointer is bigger than size of deck, then must shuffle deck
Card Deck::dealCard(){
	if(currentCard>deckLength){
		currentCard=0;
		shuffle();
	}
	currentCard++;
	return deck.at(currentCard-1);
}

//Randomly swaps two locations in vector to shuffle. Can use constant
//NUMBER_SWAPS or declare your own number
void Deck::shuffle(){
	int rand1, rand2;
	for(int i=0; i<NUMBER_SWAPS; i++){
		//generate 2 random nums (b/w 0->decklength-1), then swap the values at those two points
		rand1=rand()%deckLength;
		rand2=rand()%deckLength;
		while(rand1==rand2){
			//takes care of case where both are same number. Won't affect execution, just nice.
			rand2=rand()%deckLength;
		}
		Card tempCard = deck.at(rand2);
		deck.at(rand2) = deck.at(rand1);
		deck.at(rand1) = tempCard;
	}
}